package com.truongnha.clienttn.presentation.temp;


import org.json.JSONException;
import org.json.JSONStringer;


import android.util.Log;

/**
 * 
 * @author VAIO
 *	The ModelMark for mark viewing or mark submiting
 *  ModelMark extended the BasicModel
 *  */
public class TempMarkModel extends BasicModel {
	private String lName;
	private String fName;
	public static final int size_full = 28; //{ 8(Mieng), 8(15phut), 8(45phut), 4(ThiCuoiKy + TK(TB I,TB  II,TB CaNam) )}
	private Diem[] DiemList = new Diem[size_full];
	public TempMarkModel(String id, int index, String name, Diem[] DiemList, String lName, String fName) {
		super(id, index, name );
		// TODO Auto-generated constructor stub
		this.lName = lName;
		this.fName =  fName;
		this.DiemList = DiemList;
		//Log.d("after", this.DiemList.toString());
	}//End constructor.
	//Set all Diem for a student.
	public void setDiem(Diem[] DiemList){
		this.DiemList = DiemList;
	}//End setDiem
	//set ith Diem for a student.
	public void setDiem(Diem DiemIth, int i ){
		if(this.DiemList[i].editable){
			this.DiemList[i] = DiemIth;
		}
	}//End setDiem
	//Set all Diem for a student.
	public Diem[] getDiem(){
		return this.DiemList;
	}//End getDiem
	//Set ith Diem for a student.
	public double getDiem( int i ){
		return this.DiemList[i].diem;
	}//End getDiem
	//Get editable for Diemith
	public boolean editableIth( int i ){
		return DiemList[i].editable;
	}
	public String getDiemStringToSubmit(){
		JSONStringer string = new JSONStringer();
		try {	
			string = string.object()
					.key("lastName").value(lName)
					.key("firstName").value(fName)
					.key("id").value(Integer.parseInt(getId()))
					.key("mark").array();
			for(int i = 0 ; i < size_full ; i++ ){
				if(DiemList[i].diem == -1)
					continue;
				string = string.object()
				.key("m").value(DiemList[i].diem)
				.key("n").value(i+1)
				.key("s").value(DiemList[i].sent);
				if(DiemList[i].editable)
					string = string.key("e").value(1);
				else
					string = string.key("e").value(0);
				string = string.endObject();
			}
			
			string.endArray().endObject();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return string.toString();
	}
	public String getDiemStringToSubmit(int ith){
		JSONStringer string = new JSONStringer();
		try {	
			string = string.object()
					.key("id").value(Integer.parseInt(getId()))
					.key("mark")
						.array()
							.object()
							.key("m").value(DiemList[ith].diem)
							.key("n").value(ith+1)
							.endObject()
						.endArray()
					.endObject();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d("submit", string.toString());
		return string.toString();
	}
	
}//End class
